<template>
  <div class="app">
      <div class="chat-messages">
        <!-- <chatmessage /> -->
        <a-scrollbar style="height: calc(100vh - 140px);overflow: auto;">
          <div v-for="(item, index) in chatArr" :key="index" class="chatarr">
            <div v-if="item.status === '0'" class="reply0">
              <div class="avatar"><img width="40" src="../../../../assets/images/aigc.png" alt=""></div>
              <div class="content">{{ item.content }}</div>
            </div>
            <div v-if="item.status === '1'" class="reply1">
              <div class="avatar"><img width="40" src="../../../../assets/images/user.webp" alt=""></div>
              <div class="content">{{ item.content }}</div>
            </div>
          </div>      
        </a-scrollbar>
      </div>
      <div class="chat-input">
        <a-textarea 
          v-model:model-value="prompt" 
          class="textarea"        
          placeholder="可通过shift+回车换行" 
          :auto-size="{
            minRows:2,
            maxRows:5
          }"
          @keydown.enter="handleEnter"
          @keydown.enter.shift.prevent="handleShiftEnter"
         />
        <button @click="sendMessage"><i class="iconfont icon-jijianfasong "></i></button>
      </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue'
import { reactive } from 'vue'

const prompt = ref('')
let chatArr = reactive([
      {
        status: '0', // 0-回复，1-问题
        content: "您好，我是你的机器助手，有什么可以帮助到您的吗111111"
        +"```javascript"
        +"\n"
        + " var a = 10"
        +"\n"
        + "```\n"
        +"#hello"
      }, {
        status: '1',
        content: "我要开始提问啦！"
      }, {
        status: '0',
        content: "请问您想要问什么呢？\n"+
        "```javascript"
        +"\n"
        +"const b=5"
        +"\n"
        +"```"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '0',
        content:"我不具备实体形态和行动能力，因此我不能直接编写程序。但我可以回答关于程序的各种问题，并为你提供有关信息。如果你需要编写程序，我建议你学习一门编程语言，如Python、Java或C++，并参考相关的教程和文档。在编写程序时，你可以使用各种编程工具和框架，以提高效率和可靠性。如果你遇到具体的编程问题或困难，可以随时向我咨询，我会尽力为你提供帮助和建议。"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '1',
        content: "我要开始提问啦！"
      },{
        status: '0',
        content: "最后一行！"
      }
    ]);

async function onConConversation() {
    const message = prompt.value
    console.log(message);
}

function sendMessage() {
    onConConversation()
}

const handleShiftEnter = (event: KeyboardEvent) => {
  if (event.shiftKey && event.key === 'Enter') {
    // 按下 Shift + Enter 时在文本末尾添加换行符
    prompt.value += '\n';
  }
}

const handleEnter = (event: KeyboardEvent) => {
  if (event.key === 'Enter' && !event.shiftKey) {
    // 只按下 Enter 键，不换行，触发 sendMessage 函数
    event.preventDefault();
    sendMessage();
  }
}

</script>

<style lang="less">
.app {    
    background: linear-gradient(180deg, #f5f4f6, #e6ebf7);
    align-content: center;
    height: 100vh;
    display: flex;
    align-content: center;
    align-items: center;
    flex-direction: column;
    overflow: hidden;
}

.arco-scrollbar-container {
  width: 100%;

}
 
.chat-messages {
  display: flex;
  width: 100%;
  margin-top: 10px;
  margin-bottom: 5px;
  padding: 0;
  height: calc(100vh - 150px);
  /* background-color: #0f0; */
  overflow: hidden;
}

.arco-scrollbar {
  width: 100%;
  display: flex;
  align-content: center;
  align-items: center;
}

.chatarr {
  width: 100%;
  display: flex;
  justify-content: center;
  /* background-color: #000; */
  // 回复的样式
  .reply0{
    display: flex;
    width: 70%;
    margin-top: 20px;
    align-items: center;

    .content {
      margin-left: 10px;
      padding: 15px;
      line-height: 1.5;
      box-shadow:  
        9.2px 9px 3.6px rgba(0, 128, 255, 0.005),  /* 替换为蓝色 */  
        18.5px 18px 10px rgba(0, 128, 255, 0.012), /* 替换为蓝色 */  
        31.3px 30.5px 24.1px rgba(0, 128, 255, 0.021), /* 替换为蓝色 */  
      ;
      // max-width: 500px;
      // border: 1px solid #000;
      border-radius: 8px;
      color: #000;
      background-color: #fff;
      word-wrap: break-word;
    }
  }
  .reply1{
    display: flex;
    width: 70%;
    margin-top: 20px;
    align-items: center;

    .content {
      line-height: 1.5;
      // margin-left: 2px;
      padding: 15px;
      width: 100%;
      border: none;
      color: #000;
      word-wrap: break-word;
    }
  }
}

.avatar img {  
  border-radius: 50%; /* 这将设置图像的圆角为 50%，使其成为一个完整的圆形 */  
}

/* 底部按钮 */
.chat-input {
  // padding-top: 5px;
  padding-bottom: 70px;
  height: auto;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.textarea {
  height: auto;
  width: 60%;
  // padding-bottom: 20px;
  background-color: #fff;
  box-shadow: #f5f4f6;
  border-radius: 5px;
  outline: none;
  // overflow: hidden;
}

textarea::-webkit-scrollbar{
  display: none;
}

button {
    width: 60px;
    height: 50px;
    margin-left: 15px;
    padding: 8px;
    background-color: #007bff;
    color: #fff;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    outline: none;
}
</style>
